//
//  OfficialViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "OfficialViewController.h"
#import "OfficialCollectionViewCell.h"
#import "OfficialModel.h"
#import <AFNetworking.h>
#import "OfficialWebViewController.h"
#import "RecommendListViewController.h"
@interface OfficialViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *myArray;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;

@end

@implementation OfficialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.myArray = [NSMutableArray array];
    [self createView];
    [self createData];
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
}

- (void)createView {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
    layout.itemSize = CGSizeMake(WIDTH - 20, WIDTH / 3 + 30);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[OfficialCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([OfficialCollectionViewCell class])];
    
    [self.view addSubview:self.collectionView];
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in [responseObject[0] valueForKey:@"refs"]) {
            RefsModel *model = [[RefsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:model];
        }
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.myArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OfficialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([OfficialCollectionViewCell class]) forIndexPath:indexPath];
    RefsModel *model = self.myArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    RefsModel *model = self.myArray[indexPath.row];
    if ([model.ranklist_id integerValue] == -1) {
        OfficialWebViewController *webView = [OfficialWebViewController new];
        webView.model = model;
        [self.navigationController pushViewController:webView animated:YES];
    } else {
        RecommendListViewController *recommendVC = [RecommendListViewController new];
        recommendVC.value = [NSString stringWithFormat:@"%@",model.ranklist_id];
        NSLog(@"%@",model.ranklist_id);
        [self.navigationController pushViewController:recommendVC animated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y > self.currentOffSit_y && scrollView.contentOffset.y <= 1394) {
        [self.tabBar hiddenTabBarWithAnimation:YES];
//        self.collectionView.bounces = NO;
        self.currentOffSit_y = scrollView.contentOffset.y;
    } else if (scrollView.contentOffset.y < self.currentOffSit_y && scrollView.contentOffset.y > 0) {
        [self.tabBar showTabBarWithAnimation:YES];
//        self.collectionView.bounces = YES;
        self.currentOffSit_y = scrollView.contentOffset.y;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
